import ServerCenter from '../../Server/ServerCenter';
import SoundUtil from '../../Fantasy/Util/SoundUtil';
import LoadUtil from '../../Fantasy/Util/LoadUtil';

const {ccclass, property} = cc._decorator;

@ccclass
export default class RankLayer extends cc.Component {

    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Label)
    myRank: cc.Label = null;
    @property(cc.Label)
    myNickname: cc.Label = null;
    @property(cc.Label)
    myExp: cc.Label = null;

    @property(cc.Prefab)
    RankItem : cc.Prefab = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    imgIndex
    onEnable(){
        this.refreshRank();
    }

    refreshRank(){
        this.content.removeAllChildren();
        let data = ServerCenter.getRankInfo();
        this.myRank.string = data.rank + '';
        this.myExp.string = ServerCenter.getUserExp() + '';

        for(let i=0;i<data.list.length;i++){
            this.imgIndex = Math.ceil(Math.random()*10);
            let node = cc.instantiate(this.RankItem);
            node.parent = this.content;
            let info = data.list[i];
            let container = node.getChildByName("img").getComponent(cc.Sprite)
            let address = "Texture/Head/"+this.imgIndex+"";
            LoadUtil.LoadResImg(container,address);
            node.getChildByName('rank').getComponent(cc.Label).string = i+1+'';
            node.getChildByName('name').getComponent(cc.Label).string = info.name;
            node.getChildByName('exp').getComponent(cc.Label).string = info.score;
        }
    }

    btnCallback(target,data){
        SoundUtil.playEffect("music/click");
        if (data == 'close'){
            this.node.active = false;
        }
    }
}
